﻿#include "framework.h"
#include "Const.h"
#include "UserListView.h"

// 函数声明
static void UserListView_Delete(UserListView*);
static void UserListView_AddItem(UserListView*, LPCTSTR, int, BOOL);
static void UserListView_ChangeItemColor(UserListView*, LPCTSTR, BOOL);

/**
 * 创建用户列表视图。
 * @param id        控件 id
 * @param parent    父窗口
 */
UserListView* New_UserListView(UINT id, HWND parent) {
    UserListView* listView = (UserListView*)calloc(1, sizeof(UserListView));
    int i = 0;
    HICON icon = NULL;
    TCHAR iconFileName[MAX_PATH] = { 0 };

    // 初始化数据
    listView->mHandle = GetDlgItem(parent, id);
    SetWindowLongPtr(listView->mHandle, GWLP_USERDATA, (LONG_PTR)listView);
    listView->mIconList = ImageList_Create(ICON_SIZE, ICON_SIZE, ILC_COLOR32 | ILC_MASK, ICON_COUNT, 0);

    // 初始化函数
    listView->deleteSelf = UserListView_Delete;
    listView->addItem = UserListView_AddItem;
    listView->changeItemColor = UserListView_ChangeItemColor;

    // 创建图标列表
    for (i = 0; i < ICON_COUNT; i++) {
        _stprintf_s(iconFileName, MAX_PATH, ICON_FILE_PATH, i);
        icon = (HICON)LoadImage(NULL, iconFileName, IMAGE_ICON, ICON_SIZE, ICON_SIZE, LR_LOADFROMFILE);
        ImageList_AddIcon(listView->mIconList, icon);
    }

    // 设置图标列表
    ListView_SetImageList(listView->mHandle, listView->mIconList, LVSIL_NORMAL);
    ListView_SetImageList(listView->mHandle, listView->mIconList, LVSIL_SMALL);

    // 设置图标间距
    ListView_SetIconSpacing(listView->mHandle, ICON_SIZE + 8, 1);

    // 设置扩展风格
    ListView_SetExtendedListViewStyle(listView->mHandle, LVS_EX_FULLROWSELECT);

    return listView;
}

/**
 * 删除用户列表视图。
 * @param listView  用户列表视图
 */
static void UserListView_Delete(UserListView* listView) {
    DeleteObject(listView->mIconList);
    free(listView);
}

/**
 * 添加一个表项。
 * @param listView  用户列表视图
 * @param itemText  表项文字
 * @param iconIndex 图标索引
 * @param colorful  彩色则为 TRUE，灰色则为 FALSE
 */
static void UserListView_AddItem(UserListView* listView, LPCTSTR itemText, int iconIndex, BOOL colorful) {
    int itemIndex = ListView_GetItemCount(listView->mHandle);
    LVITEM item = { LVIF_STATE | LVIF_TEXT | LVIF_IMAGE };
    item.iItem = itemIndex;

    // 设置图标颜色
    if (!colorful) {
        item.state = LVIS_CUT;
        item.stateMask = LVIS_CUT;
    }

    // 设置图标索引
    item.iImage = iconIndex;

    // 设置文字
    item.pszText = (LPTSTR)itemText;
    item.cchTextMax = (int)_tcslen(itemText) + 1;

    // 添加表项
    ListView_InsertItem(listView->mHandle, &item);
}

/**
 * 改变某个表项的图标颜色。
 * @param listView  用户列表视图
 * @param itemText  表项的文字
 * @param colorful  彩色则为 TRUE，灰色则为 FALSE
 */
static void UserListView_ChangeItemColor(UserListView* listView, LPCTSTR itemText, BOOL colorful) {
    LVFINDINFO findInfo = { LVFI_STRING };
    int itemIndex = -1;

    // 查找表项
    findInfo.psz = itemText;
    itemIndex = ListView_FindItem(listView->mHandle, -1, &findInfo);
    if (itemIndex < 0) {
        return;
    }

    // 设置图标颜色
    if (colorful) {
        ListView_SetItemState(listView->mHandle, itemIndex, 0, LVIS_CUT);
    } else {
        ListView_SetItemState(listView->mHandle, itemIndex, LVIS_CUT, LVIS_CUT);
    }
}
